# coding=utf-8
"""
    @project: python-learning-notes
    @Author：frank
    @file： Run.py
    @date：2023/9/7 20:21
"""
from flask import Flask, render_template, request, make_response

app = Flask(__name__, template_folder='templates', static_url_path='/static', static_folder='static')


@app.route('/')
def index():
    return render_template('index.html')


@app.route('/request')
def request_views():
    print(dir(request))
    # 获取请求方案（协议）
    scheme = request.scheme
    # 获取请求方式
    method = request.method
    # 获取get请求方式提交的数据
    args = request.args
    # 获取post请求方式提交的数据
    form = request.form
    # 获取任意一种请求方式提交的数据
    values = request.values
    # data
    data = request.data
    # 获取cookies
    cookies = request.cookies
    # headers
    headers = request.headers
    user_agent = headers.get('User-Agent')
    # path
    path = request.path
    return render_template('request.html', params=locals())


@app.route('/form')
def from_views():
    return render_template('03-form.html')


@app.route('/form_do')
def form_do():
    if request.method == 'GET':
        # 获取form表单提交过来的数据
        uname = request.args.get('uname')
        upwd = request.args.get('upwd')
        print('用户名称:%s，用户密码:%s' % (uname, upwd))
    return '获取表单数据成功'


@app.route('/post')
def post_view():
    return render_template('04-form.html')


@app.route('/post_do', methods=['post'])
def post_do():
    uname = request.form.get('uname')
    upwd = request.form.get('upwd')
    print('用户名称:%s，用户密码:%s' % (uname, upwd))
    return '获取 数据成功'

@app.route('/response')
def response_views():
    # 创建响应对象给客户端
    #resp = make_response('使用响应对象响应回去的内容')

    #创建响应对象，并赋值响应的模板
    #将响应对象进行返回
    resp = make_response(render_template('04-form.html'))

    return resp

if __name__ == "__main__":
    app.run(host='0.0.0.0', debug=True)
